package com.hclc.config;

import com.baomidou.mybatisplus.core.enums.IEnum;
import com.google.common.collect.Maps;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import java.util.Map;

/**
 * 编码 -> 枚举 转化器工厂类
 *
 * @ClassName StringCodeToEnumConverterFactory
 * @Author: yurj
 * @Mail：1638234804@qq.com
 * @Date: Create in 16:28 2020/11/12
 * @version: 1.0
 */
public class StringCodeToEnumConverterFactory implements ConverterFactory<String, IEnum> {
    private static final Map<Class, Converter> CONVERTERS = Maps.newHashMap();

    /**
     * 转换器存取
     *
     * @param targetType
     * @return org.springframework.core.convert.converter.Converter<java.lang.String, T>
     * @author yurj
     * @version 1.0
     * @date 2020/11/12 16:32
     */
    @Override
    public <T extends IEnum> Converter<String, T> getConverter(Class<T> targetType) {
        Converter<String, T> converter = CONVERTERS.get(targetType);
        if (converter == null) {
            converter = new StringToEnumConverter<>(targetType);
            CONVERTERS.put(targetType, converter);
        }
        return converter;
    }
}
